import 'package:get/get.dart';
import 'package:audioplayers/audioplayers.dart';
import '../models/music.dart';

class MusicController extends GetxController {
  final AudioPlayer audioPlayer = AudioPlayer();
  Rx<Music?> currentMusic = Rx<Music?>(null);
  Rx<bool> isPlaying = false.obs;

  Future<void> playMusic(Music music) async {
    currentMusic.value = music;
    await audioPlayer.setSourceUrl(music.url);
    await audioPlayer.play(UrlSource(music.url));
    isPlaying.value = true;
  }

  Future<void> pauseMusic() async {
    await audioPlayer.pause();
    isPlaying.value = false;
  }

  Future<void> resumeMusic() async {
    await audioPlayer.resume();
    isPlaying.value = true;
  }

  @override
  void onClose() {
    audioPlayer.dispose();
    super.onClose();
  }
}
